home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------
- #
- # NewsWatcher - Macintosh NNTP Client Application
- #
- # Written by Steven Falkenburg
- # ©1990 Apple Computer, Inc.
- #
- #-----------------------------------------------------------
- #
- # scrollstuff.c
- #
- # This code module supports scrolling for textedit fields
- # of text located in resizeable windows.
- #
- #-----------------------------------------------------------*/
-
- #pragma segment userint
-
- #include "compat.h"
- #include <stdlib.h>
-
- #ifdef PROTOS
-
- #include <Types.h>
- #include <Windows.h>
- #include <TextEdit.h>
- #include <Controls.h>
- #include <OSUtils.h>
- #include <Events.h>
- #include <StdLib.h>
- #include <Lists.h>
- #endif
-
- #include "nntp.h"
- #include "ScrollStuff.h"
-
-
- ControlHandle hScrollCont(WindowPtr window)
- {
- if (GetCRefCon( ((WindowPeek) window)->controlList ) == kVRef )
- return((**(((WindowPeek) window)->controlList)).nextControl);
- else
- return(((WindowPeek) window)->controlList);
- }
-
-
- ControlHandle vScrollCont(WindowPtr window)
- {
- if (GetCRefCon( ((WindowPeek) window)->controlList ) == kHRef )
- return((**(((WindowPeek) window)->controlList)).nextControl);
- else
- return(((WindowPeek) window)->controlList);
- }
-
-
- long sign(long longVar)
- {
- if (longVar>=0)
- return(1);
- else
- return(-1);
- }
-
-
- short LinesInText(TEHandle theTE)
- {
- short lines;
- Handle textHandle;
-
- lines = (**theTE).nLines;
- textHandle = (**theTE).hText;
- if ( (**theTE).teLength > 0 ) {
- if ( *(*textHandle+((**theTE).teLength - 1)) == CR )
- lines++;
- return(lines);
- }
- }
-
-
- void MoveText(WindowPtr window,ControlHandle theControl)
- {
- long viewTop,destTop;
- long scrollValue;
- long scrollDiff,oldScroll,newScroll;
- short height;
- TEHandle theTE;
-
- theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(window))->data);
-
- if (theControl == vScrollCont(window)) {
- viewTop = (**theTE).viewRect.top;
- destTop = (**theTE).destRect.top;
- oldScroll = viewTop - destTop;
- scrollValue = GetCtlValue(theControl);
- height = (**theTE).lineHeight;
- newScroll = scrollValue * height;
- scrollDiff = oldScroll - newScroll;
- if (abs(scrollDiff)>32000) {
- TEScroll(0L,sign(scrollDiff) * 32000,theTE);
- SysBeep(30);
- }
- else if (scrollDiff != 0)
- TEScroll(0L,scrollDiff,theTE);
- }
- else {
- viewTop = (**theTE).viewRect.left;
- destTop = (**theTE).destRect.left;
- oldScroll = viewTop - destTop;
- scrollValue = GetCtlValue(theControl);
- newScroll = scrollValue * kTextWidth;
- scrollDiff = oldScroll - newScroll;
- if (abs(scrollDiff)>32000) {
- TEScroll(sign(scrollDiff) * 32000,0L,theTE);
- SysBeep(30);
- }
- else if (scrollDiff != 0)
- TEScroll(scrollDiff,0L,theTE);
- }
- }
-
-
- void AdjustScrollBar(WindowPtr window)
- {
- short windowLines,currentLines;
- TEHandle theTE;
-
- theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(window))->data);
-
- windowLines = ((**theTE).viewRect.bottom - (**theTE).viewRect.top) / (**theTE).lineHeight;
- if ((currentLines = LinesInText(theTE)) >= windowLines)
- SetCtlMax(vScrollCont(window),currentLines - windowLines);
- else
- SetCtlMax(vScrollCont(window),0L);
-
- windowLines = ((**theTE).viewRect.right - (**theTE).viewRect.left) / kTextWidth;
- if (kMaxColumns >= windowLines)
- SetCtlMax(hScrollCont(window),kMaxColumns);
- else
- SetCtlMax(hScrollCont(window),0L);
- }
-
-
- void ScrollChar(WindowPtr window,short charPos,Boolean toBottom)
- {
- short theLine,windowLines;
- TEHandle theTE;
-
- theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(window))->data);
-
- theLine = 0;
- while ( (**theTE).lineStarts[theLine + 1] <= charPos )
- theLine++;
- if (toBottom) {
- windowLines = ((**theTE).viewRect.bottom - (**theTE).viewRect.top) / (**theTE).lineHeight;
- theLine = theLine - (windowLines - 1);
- }
- SetCtlValue(vScrollCont(window),theLine);
- MoveText(window,hScrollCont(window));
- MoveText(window,vScrollCont(window));
- }
-
-
- void CheckInsertion(WindowPtr window)
- {
- short topLine,bottomLine,windowLines;
- TEHandle theTE;
-
- theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(window))->data);
-
- windowLines = ((**theTE).viewRect.bottom - (**theTE).viewRect.top) / (**theTE).lineHeight;
- topLine = GetCtlValue(vScrollCont(window));
- bottomLine = topLine + windowLines;
- if (GetCtlMax(vScrollCont(window)) == 0)
- MoveText(window,vScrollCont(window));
- else if ((**theTE).selEnd < (**theTE).lineStarts[topLine])
- ScrollChar(window,(**theTE).selStart,false);
- else if ((**theTE).selStart >= (**theTE).lineStarts[bottomLine])
- ScrollChar(window,(**theTE).selEnd,true);
- }
-
-
- pascal void scroll_action(ControlHandle scrollBar,short part)
- {
- short scrollAmt = 0,pageSize;
- short theCtlValue;
- TEHandle theTE;
-
- theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(FrontWindow()))->data);
-
- pageSize = ((**theTE).viewRect.bottom - (**theTE).viewRect.top) / (**theTE).lineHeight - 1;
- switch (part) {
- case inUpButton:
- scrollAmt = -1;
- break;
- case inDownButton:
- scrollAmt = 1;
- break;
- case inPageUp:
- scrollAmt = -pageSize;
- break;
- case inPageDown:
- scrollAmt = pageSize;
- break;
- }
- theCtlValue = GetCtlValue(scrollBar)+scrollAmt;
- if (part == inPageDown || part == inPageUp || theCtlValue <= GetCtlMax(scrollBar) && theCtlValue >= GetCtlMin(scrollBar)) {
- SetCtlValue(scrollBar,GetCtlValue(scrollBar)+scrollAmt);
- MoveText(FrontWindow(),scrollBar);
- }
- }
-
-
- void DoScrollers(ControlHandle scrollBar,short part,Point localMouse)
- {
- if (part == inThumb) {
- TrackControl(scrollBar,localMouse,nil);
- MoveText(FrontWindow(),scrollBar);
- }
- else
- TrackControl(scrollBar,localMouse,(ProcPtr) scroll_action);
- }
-
-
- pascal Boolean AutoScroll(void)
- {
- RgnHandle oldClip;
- Point mouseLoc;
- Rect textRect,tempRect;
- short deltaX = 0,deltaY = 0;
- ControlHandle sBar;
- TEHandle theTE;
-
- theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(FrontWindow()))->data);
-
- oldClip = NewRgn();
- GetClip(oldClip);
- tempRect = FrontWindow()->portRect;
- ClipRect(&tempRect);
- GetMouse(&mouseLoc);
- textRect = (**theTE).viewRect;
- if (mouseLoc.v < textRect.top)
- deltaY = -1;
- else if (mouseLoc.h < textRect.left)
- deltaX = -1;
- else if (mouseLoc.v > textRect.bottom)
- deltaY = 1;
- else if (mouseLoc.h > textRect.right)
- deltaX = 1;
- if (deltaY) {
- sBar = vScrollCont(FrontWindow());
- if (GetCtlValue(sBar)+deltaY <= GetCtlMax(sBar) &&
- GetCtlValue(sBar)+deltaY >= GetCtlMin(sBar)) {
- SetCtlValue(sBar,GetCtlValue(sBar)+deltaY);
- MoveText(FrontWindow(),sBar);
- }
- }
- else if (deltaX) {
- sBar = hScrollCont(FrontWindow());
- if (GetCtlValue(sBar)+deltaX <= GetCtlMax(sBar) &&
- GetCtlValue(sBar)+deltaX >= GetCtlMin(sBar)) {
- SetCtlValue(sBar,GetCtlValue(sBar)+deltaX);
- MoveText(FrontWindow(),sBar);
- }
- }
- SetClip(oldClip);
- DisposeRgn(oldClip);
- return true;
- }
-